<?php
class PageesController extends AppController {
	
	var $name = 'Pagees';
	var $uses = array('Pagee');
	var $layout = 'admin';
	var $helpers = array('Html', 'Form', 'Javascript', 'Tinymce', 'Time');
	var $components = array('General');
	
	function index()
	{
		$condition = array();
		$this->paginate = array('limit' => 15, 'order' => array('Pagee.created_date' => 'desc'));
		
		//Get condition
		if (!empty($this->passedArgs))
		{
			if (isset($this->passedArgs['Search.name']))
			{
				$condition[]["Pagees.title LIKE"] = "%{$this->passedArgs['Search.name']}%";
				$this->data['Search']['name'] = $this->passedArgs['Search.name'];
			}
		}
		$this->set('pagees', $this->paginate('Pagee', $condition));
	}
	function add()
	{
		if (!empty($this->data))
		{
			//Set created data
			$this->data['Pagee']['created_date'] = date('Y-m-d h:m:s');
			if ($this->Pagee->save($this->data)) 
			{
				$this->Session->setFlash('Tin đã được thêm');
				$this->redirect('/pagees/index');
			} 
			else 
			{
				$this->Session->setFlash('Lỗi trong quá trình lưu dữ liệu');
			}
		}
	}
	function edit($id=null)
	{
		if(! empty ($this->data))
		{
			if($this->Pagee->save ($this->data))
			{
				$this->Session->setFlash (__ ('The Page has been saved', true));
				//$this->redirect (array('action' => 'index'));
			}else
			{
				$this->Session->setFlash (__ ('The User could not be saved. Please, try again.', true));
			}
		}
		if(empty ($this->data))
		{
			
			//Get condition
			if (!empty($this->passedArgs))
			{
				if (isset($this->passedArgs['Search.name']))
				{
					$this->data = $this->Pagee->find("Pagee.title like '".$this->passedArgs['Search.name']."'");
				}
				else
				{
					$this->data = $this->Pagee->read (null, $id);
				}
			}
			else
			{
				if(!$id)
				{
					$this->Session->setFlash (__ ('Invalid page', true));
					$this->redirect (array('action' => 'index'));
				}
			}
			
		}
	}
	function delete($id=null)
	{
		if (!$id)
		{
			$this->Session->setFlash('Không đúng ID');
			$this->redirect ('index');
		}
		if ($this->Pagee->del($id))
		{
			$this->Session->setFlash('Đã xóa xong');
		}
		$this->redirect('index');
	}
}
?>